<?php
/**
 * APP版本列表
 * User: xwc
 * Date: 2023/01/15
 */

namespace App\Service\Core\Sys\Config;

use App\Component\ListService\ListAbstract;
use App\Model\Core\Sys\AppVersionModel;

/**
 * @method $this setAppPlatform($val)
 * @method $this setTargetVersion($val)
 * @method $this setMinVersion($val)
 * @method $this setCreatedAtBegin($val)
 * @method $this setCreatedAtEnd($val)
 * @package App\Service\Core\Sys\Config\AppVersionListService
 */
class AppVersionListService extends ListAbstract
{
    //允许获取的字段
    protected array $allow_fields = ['*'];
    //允许获取的扩展字段
    protected array $allow_ext_fields = ['app_file_url'];
    //默认返回的字段
    protected array $default_fields = [];
    protected array $allow_sort = ['created_at', 'id', 'target_version'];
    protected array $default_sort_by = ['id' => 'desc'];

    protected function getQueryObj()
    {
        // TODO: Implement getQueryObj() method.
        $query_obj = AppVersionModel::getInstance()->getConnectionTable();
        //平台
        $app_platform = $this->getParam('app_platform');
        if (!empty($app_platform)) {
            $query_obj->where('app_platform', 'like', "%{$app_platform}%");
        }
        //目标版本号
        $target_version = $this->getParam('target_version');
        if (!empty($target_version)) {
            $query_obj->where('target_version', $target_version);
        }
        //最低版本号
        $min_version = $this->getParam('min_version');
        if (!empty($min_version)) {
            $query_obj->where('min_version', $min_version);
        }
        //开始时间
        $created_at_begin = $this->getParam('created_at_begin');
        if (!empty($created_at_begin)) {
            $created_at_begin = date('Y-m-d H:i:s', strtotime($created_at_begin));
            $query_obj->where('created_at', '>=', $created_at_begin);
        }
        //结束时间
        $created_at_end = $this->getParam('created_at_end');
        if (!empty($created_at_end)) {
            $created_at_end = date('Y-m-d H:i:s', strtotime($created_at_end));
            $query_obj->where('created_at', '<=', $created_at_end);
        }
        return $query_obj;
    }

    /**
     * 获取扩展字段
     * @param array $row
     * @return array
     */
    protected function getExtField(array $row)
    {
        if (easy_in_array('app_file_url', $this->ext_fields)) {
            $row['app_file_url'] = easy_cloud_attached($row['app_file']);
        }
        return $row;
    }

}
